<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Lua 备忘清单
 &#x26;  lua cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="包含最重要概念、函数、方法等的 Lua 备忘单。 初学者的完整快速参考。

入门，为开发人员分享快速参考备忘单。">
<meta keywords="lua,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/lua.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.2"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="lua-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 32 32" height="1em" width="1em">
  <path d="m.505 13.839-.36-.052c-.067.459-.109.927-.135 1.391l.364.02c.027-.452.068-.911.131-1.359zm5.011-9.454-.245-.271c-.344.307-.683.635-.995.973l.265.256c.313-.333.636-.656.975-.959zm2.177-1.64-.199-.313c-.391.251-.781.516-1.145.792l.219.292a14.89 14.89 0 0 1 1.125-.771zM2.432 24.489c.245.396.511.781.792 1.151l.292-.219a15.077 15.077 0 0 1-.771-1.124zm-.927-2.62-.344.141c.172.421.365.844.579 1.255l.005.016.328-.167-.011-.016a15.065 15.065 0 0 1-.557-1.229zM.396 12.412l.359.083c.105-.443.224-.885.365-1.312l-.349-.115a15.9 15.9 0 0 0-.375 1.344zm-.032 4.151L0 16.574c.016.463.052.932.109 1.391l.364-.043a14.992 14.992 0 0 1-.109-1.359zm-.02 2.781c.093.453.213.907.349 1.349l.348-.105c-.129-.437-.244-.88-.344-1.323zM15.432.364 15.416 0c-.463.016-.932.052-1.389.109l.047.364c.448-.057.905-.093 1.359-.109zm15.532 11.057c.129.433.244.881.339 1.324l.359-.079a14.795 14.795 0 0 0-.349-1.355zm-7.469-9.566a15.542 15.542 0 0 0-1.26-.6l-.147.333c.423.177.833.376 1.235.589zM20.921.76a17.402 17.402 0 0 0-1.343-.364l-.084.353c.443.105.885.224 1.317.365zM8.901 2.063c.396-.203.807-.391 1.219-.557l-.141-.344a15.88 15.88 0 0 0-1.244.573l-.021.011.167.328zm22.735 13.39.369-.016a21.153 21.153 0 0 0-.109-1.396l-.365.047c.053.448.089.907.105 1.365zM18.203.14a15.693 15.693 0 0 0-1.391-.129l-.015.364c.453.021.911.068 1.359.131zm-5.479.563-.079-.359c-.452.093-.905.213-1.348.349l.109.348c.432-.129.875-.244 1.317-.339zM2.563 7.292l.307.197c.245-.375.511-.749.787-1.109l-.287-.229c-.287.371-.557.756-.807 1.141zM1.255 9.755l.339.147c.177-.417.376-.833.589-1.235l-.328-.172c-.215.412-.417.833-.6 1.26zm23.042 19.5.192.313c.396-.245.781-.511 1.151-.792l-.219-.292c-.359.271-.739.537-1.124.771zm4.031-3.63.292.229c.287-.365.557-.751.807-1.141l-.301-.197c-.251.38-.516.749-.797 1.109zm-1.855 1.995.245.271c.344-.307.683-.64.995-.973l-.265-.256c-.308.333-.641.651-.975.959zm3.339-4.281.324.172c.219-.412.421-.833.599-1.26l-.339-.141c-.177.417-.369.828-.584 1.229zm1.677-5.167.365.052c.063-.464.109-.927.135-1.391l-.364-.021c-.027.453-.073.912-.136 1.36zm-.614 2.656.355.115c.14-.443.265-.896.369-1.349l-.359-.083c-.1.448-.224.891-.365 1.317zm-.938-11.932c.203.407.396.823.568 1.244l.339-.14c-.172-.427-.371-.86-.584-1.271zM11.063 31.224c.443.14.891.265 1.344.369l.083-.353a15.487 15.487 0 0 1-1.312-.365zm2.718.631c.459.063.927.109 1.391.135l.016-.369a15.064 15.064 0 0 1-1.36-.131zm9.318-1.923c-.401.204-.812.396-1.235.563l.141.344c.421-.177.855-.365 1.26-.579l.005-.005-.161-.328zM6.151 28.625c.365.281.751.557 1.141.801l.197-.301c-.38-.251-.755-.516-1.115-.792zm10.406 3.011.011.364c.463-.021.932-.052 1.396-.115l-.047-.359c-.448.057-.907.093-1.36.109zM8.495 30.14c.407.215.833.417 1.26.6l.141-.339a17.761 17.761 0 0 1-1.229-.589zm-4.386-3.421c.312.349.64.683.979.995l.251-.265a15.397 15.397 0 0 1-.953-.975zm15.151 4.584.079.359c.453-.099.907-.219 1.355-.355l-.109-.348a21.46 21.46 0 0 1-1.324.344zM28.24.172a3.583 3.583 0 0 0-3.584 3.583 3.584 3.584 0 0 0 3.584 3.589c4.781 0 4.781-7.172 0-7.172zm-7.808 20.421c0 .631.417.88 1.136.88.896 0 1.729-.525 1.729-1.353v-.86c-.308.147-.505.188-1.475.324-.979.145-1.391.405-1.391 1.009zM16 3.755c-6.76 0-12.24 5.48-12.24 12.24S9.24 28.24 16 28.24s12.24-5.485 12.24-12.245c0-6.755-5.48-12.24-12.24-12.24zM12.683 22.12H7.694v-8.032h1.025v7.125h3.969v.907zm5.51 0h-.829v-.896c-.541.76-1.077 1.057-1.927 1.057-1.125 0-1.839-.62-1.839-1.579v-4.359h.912v4c0 .692.463 1.135 1.192 1.135.959 0 1.573-.771 1.573-1.948v-3.187h.917zm-.709-11.193a3.583 3.583 0 1 1 7.166 0 3.583 3.583 0 0 1-7.166 0zm7.423 10.474v.692c-.297.079-.437.1-.625.1a.976.976 0 0 1-.953-.761 2.695 2.695 0 0 1-1.959.849c-1.172 0-1.896-.615-1.896-1.661 0-.703.339-1.224 1.011-1.5.36-.141.552-.188 1.855-.349.729-.088.959-.255.959-.64v-.245c0-.547-.464-.859-1.287-.859-.865 0-1.276.323-1.36 1.025h-.921c.057-1.333.916-1.869 2.312-1.869 1.423 0 2.167.547 2.167 1.572v3.219c0 .287.177.448.5.448.052 0 .099 0 .199-.02z"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#lua-备忘清单"><span class="icon icon-link"></span></a>Lua 备忘清单</h1><div class="wrap-body">
<p>包含最重要概念、函数、方法等的 <a href="http://www.lua.org">Lua</a> 备忘单。 初学者的完整快速参考。</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#下载">下载</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#其它下载方式">其它下载方式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#hello-world">hello world</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#运行">运行</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#注释">注释</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#单行注释">单行注释</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#多行注释">多行注释</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#type-函数">type() 函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#number">number</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#string">string</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#">[[]]</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#字符串连接">字符串连接(..)</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#字符串长度">字符串长度(#)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#table">table</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#迭代-table">迭代 table</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#指定键">指定键</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量">变量</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#默认值">默认值</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#全局和局部变量">全局和局部变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#赋值">赋值</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#交换变量">交换变量</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#赋值个数不一致">赋值个数不一致</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运算符">运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类型转换">类型转换</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#goto-语法">goto 语法</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#条件语句">条件语句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运算符-1">运算符</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#关系运算符">关系运算符</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#逻辑运算符">逻辑运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#while-循环">while 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#if-语句">if 语句</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#if--elseif--else">if .. elseif() .. else</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#for-循环">for 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#repeatuntil-循环">repeat...until 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#break">break</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#函数">函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#初始化">初始化</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#返回值">返回值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#参数">参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多个返回值">多个返回值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#可变参数">可变参数(...)</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#字符串">字符串</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串方法">字符串方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#正则匹配">正则匹配</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#match">match</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#gmatch">gmatch</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#数学方法">数学方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#常用方法">常用方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#工具方法">工具方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#其它方法">其它方法</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#table-1">table</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#初始化数组">初始化数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多维数组">多维数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#初始化-table">初始化 table</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#table-方法">table 方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#迭代器">迭代器</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#无状态的迭代器">无状态的迭代器</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#for-循环迭代器">for 循环迭代器</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#模块">模块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义模块">定义模块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#导入模块">导入模块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#私有函数">私有函数</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="下载"><a aria-hidden="true" tabindex="-1" href="#下载"><span class="icon icon-link"></span></a>下载</h3><div class="wrap-body">
<p>macos 使用 homebrew 下载</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ brew <span class="token function">install</span> lua
</span></code></pre>
<h4 id="其它下载方式"><a aria-hidden="true" tabindex="-1" href="#其它下载方式"><span class="icon icon-link"></span></a>其它下载方式</h4>
<ul>
<li><a href="https://luabinaries.sourceforge.net/download.html">下载地址</a> <em>(sourceforge.net)</em></li>
</ul>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 查看 lua 是否安装成功</span>
</span><span class="code-line">$ lua <span class="token parameter variable">-v</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="hello-world"><a aria-hidden="true" tabindex="-1" href="#hello-world"><span class="icon icon-link"></span></a>hello world</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">#!/usr/bin/env lua</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Hello World!"</span><span class="token punctuation">)</span>
</span></code></pre>
<h4 id="运行"><a aria-hidden="true" tabindex="-1" href="#运行"><span class="icon icon-link"></span></a>运行</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ lua ./hello.lua
</span><span class="code-line"><span class="token comment"># 或者也可以像 bash 脚本一样</span>
</span><span class="code-line">$ <span class="token function">chmod</span> +x hello.lua
</span><span class="code-line">./hello.lua
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="注释"><a aria-hidden="true" tabindex="-1" href="#注释"><span class="icon icon-link"></span></a>注释</h3><div class="wrap-body">
<h4 id="单行注释"><a aria-hidden="true" tabindex="-1" href="#单行注释"><span class="icon icon-link"></span></a>单行注释</h4>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">-- 以两个减号开始</span>
</span></code></pre>
<h4 id="多行注释"><a aria-hidden="true" tabindex="-1" href="#多行注释"><span class="icon icon-link"></span></a>多行注释</h4>
<p>多行注释以 <code>--[[</code> 开头, 以 <code>]]</code> 结尾</p>
<pre class="wrap-text "><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">--[[
</span></span><span class="code-line"><span class="token comment">
</span></span><span class="code-line"><span class="token comment">]]</span>
</span></code></pre>
<!--rehype:className=wrap-text -->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="type-函数"><a aria-hidden="true" tabindex="-1" href="#type-函数"><span class="icon icon-link"></span></a>type() 函数</h3><div class="wrap-body">
<p>使用 <code>type()</code> 函数可以判断变量或者值的类型</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">type</span><span class="token punctuation">(</span><span class="token keyword">true</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">-- boolean</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">type</span><span class="token punctuation">(</span><span class="token keyword">nil</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">-- nil</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="number"><a aria-hidden="true" tabindex="-1" href="#number"><span class="icon icon-link"></span></a>number</h3><div class="wrap-body">
<p>Lua 默认只有一种 number 类型 double (双精度) 类型</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>2e <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="string"><a aria-hidden="true" tabindex="-1" href="#string"><span class="icon icon-link"></span></a>string</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">-- 使用 ''</span>
</span><span class="code-line"><span class="token keyword">local</span> str1 <span class="token operator">=</span> <span class="token string">'str1'</span>
</span><span class="code-line"><span class="token comment">-- 使用 ""</span>
</span><span class="code-line"><span class="token keyword">local</span> str2 <span class="token operator">=</span> <span class="token string">"str2"</span>
</span></code></pre>
<h4 id=""><a aria-hidden="true" tabindex="-1" href="#"><span class="icon icon-link"></span></a><code>[[]]</code></h4>
<p>使用 <code>[[]]</code> 跨行表示多个字符串</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> html <span class="token operator">=</span> <span class="token string">[[
</span></span><span class="code-line"><span class="token string">&#x3C;html>
</span></span><span class="code-line"><span class="token string">&#x3C;head>&#x3C;/head>
</span></span><span class="code-line"><span class="token string">&#x3C;body>
</span></span><span class="code-line"><span class="token string">  &#x3C;a href="https://www.twle.cn/">
</span></span><span class="code-line"><span class="token string">    简单编程
</span></span><span class="code-line"><span class="token string">  &#x3C;/a>
</span></span><span class="code-line"><span class="token string">&#x3C;/body>
</span></span><span class="code-line"><span class="token string">&#x3C;/html>
</span></span><span class="code-line"><span class="token string">]]</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>html<span class="token punctuation">)</span>
</span></code></pre>
<h4 id="字符串连接"><a aria-hidden="true" tabindex="-1" href="#字符串连接"><span class="icon icon-link"></span></a>字符串连接(<code>..</code>)</h4>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"a"</span> <span class="token operator">..</span> <span class="token string">'b'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">-- ab</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token number">157</span> <span class="token operator">..</span> <span class="token number">428</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">-- 157428</span>
</span></code></pre>
<h4 id="字符串长度"><a aria-hidden="true" tabindex="-1" href="#字符串长度"><span class="icon icon-link"></span></a>字符串长度(<code>#</code>)</h4>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token operator">#</span><span class="token string">"string"</span><span class="token punctuation">)</span> <span class="token comment">-- 6</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="table"><a aria-hidden="true" tabindex="-1" href="#table"><span class="icon icon-link"></span></a>table</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> table <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</span></code></pre>
<h4 id="迭代-table"><a aria-hidden="true" tabindex="-1" href="#迭代-table"><span class="icon icon-link"></span></a>迭代 table</h4>
<p>默认的初始索引会从 1 开始</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> array <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">"apple"</span><span class="token punctuation">,</span> <span class="token string">"pear"</span><span class="token punctuation">,</span> <span class="token string">"orange"</span><span class="token punctuation">,</span> <span class="token string">"grape"</span> <span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>array<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">-- apple</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">for</span> k<span class="token punctuation">,</span> v <span class="token keyword">in</span> <span class="token function">pairs</span><span class="token punctuation">(</span>array<span class="token punctuation">)</span> <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>k <span class="token operator">..</span> <span class="token string">" : "</span> <span class="token operator">..</span> v<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment">-- 1 : apple</span>
</span><span class="code-line"><span class="token comment">-- 2 : pear</span>
</span><span class="code-line"><span class="token comment">-- 3 : orange</span>
</span><span class="code-line"><span class="token comment">-- 4 : grape</span>
</span></code></pre>
<h4 id="指定键"><a aria-hidden="true" tabindex="-1" href="#指定键"><span class="icon icon-link"></span></a>指定键</h4>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> array <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</span><span class="code-line">array<span class="token punctuation">.</span>one <span class="token operator">=</span> <span class="token string">"apple"</span>
</span><span class="code-line">array<span class="token punctuation">[</span><span class="token string">"two"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">"peach"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>array<span class="token punctuation">.</span>one<span class="token punctuation">)</span> <span class="token comment">-- apple</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>array<span class="token punctuation">.</span>two<span class="token punctuation">)</span> <span class="token comment">-- peach</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="变量"><a aria-hidden="true" tabindex="-1" href="#变量"><span class="icon icon-link"></span></a>变量</h3><div class="wrap-body">
<h4 id="默认值"><a aria-hidden="true" tabindex="-1" href="#默认值"><span class="icon icon-link"></span></a>默认值</h4>
<p>变量的默认值均是 nil</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">#!/usr/bin/env lua</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span> <span class="token comment">-- nil</span>
</span></code></pre>
<h4 id="全局和局部变量"><a aria-hidden="true" tabindex="-1" href="#全局和局部变量"><span class="icon icon-link"></span></a>全局和局部变量</h4>
<p>Lua 中的变量全是全局变量，那怕是语句块或是函数里，除非用 local 显式声明为局部变量</p>
<pre class="wrap-text "><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">#!/usr/bin/env lua</span>
</span><span class="code-line"><span class="token keyword">function</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">local</span> b <span class="token operator">=</span> <span class="token number">12</span>
</span><span class="code-line">  a <span class="token operator">=</span> <span class="token number">23</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment">-- 23</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span> <span class="token comment">-- nil</span>
</span></code></pre>
<!--rehype:className=wrap-text -->
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="赋值"><a aria-hidden="true" tabindex="-1" href="#赋值"><span class="icon icon-link"></span></a>赋值</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line">a <span class="token operator">=</span> <span class="token string">"hello "</span> <span class="token operator">..</span> <span class="token string">"world"</span> <span class="token comment">-- 改变 变量</span>
</span><span class="code-line">t<span class="token punctuation">.</span>n <span class="token operator">=</span> t<span class="token punctuation">.</span>n <span class="token operator">+</span> <span class="token number">1</span>  <span class="token comment">-- 改变 table</span>
</span></code></pre>
<hr>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">-- 给多个变量赋值</span>
</span><span class="code-line">a<span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token operator">*</span>a  <span class="token comment">--> a=10; b=20</span>
</span></code></pre>
<h4 id="交换变量"><a aria-hidden="true" tabindex="-1" href="#交换变量"><span class="icon icon-link"></span></a>交换变量</h4>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> x<span class="token punctuation">,</span> y <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span>
</span><span class="code-line">x<span class="token punctuation">,</span> y <span class="token operator">=</span> y<span class="token punctuation">,</span> x
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span> <span class="token comment">-- 3, 1</span>
</span></code></pre>
<hr>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> tab <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line">tab<span class="token punctuation">.</span>one <span class="token operator">=</span> <span class="token number">2</span>
</span><span class="code-line">tab<span class="token punctuation">.</span>two <span class="token operator">=</span> <span class="token number">1</span>
</span><span class="code-line">
</span><span class="code-line">tab<span class="token punctuation">[</span><span class="token string">"one"</span><span class="token punctuation">]</span><span class="token punctuation">,</span> tab<span class="token punctuation">[</span><span class="token string">"two"</span><span class="token punctuation">]</span> <span class="token operator">=</span> tab<span class="token punctuation">.</span>two<span class="token punctuation">,</span> tab<span class="token punctuation">.</span>one
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>tab<span class="token punctuation">.</span>one<span class="token punctuation">,</span> tab<span class="token punctuation">.</span>two<span class="token punctuation">)</span> <span class="token comment">-- 1    2</span>
</span></code></pre>
<h4 id="赋值个数不一致"><a aria-hidden="true" tabindex="-1" href="#赋值个数不一致"><span class="icon icon-link"></span></a>赋值个数不一致</h4>
<ul class="style-round">
<li>
<p>如果变量个数<strong>大于</strong>值的个数，按变量个数补足 nil</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line">a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>c<span class="token punctuation">)</span>      <span class="token comment">--> 1   3   nil</span>
</span></code></pre>
</li>
<li>
<p>如果变量个数<strong>小于</strong>值的个数，多余的值会被忽略</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line">a <span class="token operator">=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token keyword">local</span> a<span class="token punctuation">,</span> b <span class="token operator">=</span> a<span class="token punctuation">,</span> a <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> a <span class="token operator">+</span> <span class="token number">2</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token comment">--> 1   2</span>
</span></code></pre>
</li>
</ul>
<!--rehype:className=style-round-->
<!--rehype:className=wrap-text -->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="运算符"><a aria-hidden="true" tabindex="-1" href="#运算符"><span class="icon icon-link"></span></a>运算符</h3><div class="wrap-body">





































<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>+</code></td><td align="left">加法</td></tr><tr><td align="left"><code>-</code></td><td align="left">减法</td></tr><tr><td align="left"><code>*</code></td><td align="left">乘法</td></tr><tr><td align="left"><code>/</code></td><td align="left">除法</td></tr><tr><td align="left"><code>%</code></td><td align="left">取余，求出除法的余数</td></tr><tr><td align="left"><code>^</code></td><td align="left">乘幂，计算次方</td></tr><tr><td align="left"><code>-</code></td><td align="left">负号，取负值</td></tr></tbody></table>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> a<span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">+</span> b<span class="token punctuation">)</span> <span class="token comment">-- 7</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">-</span> b<span class="token punctuation">)</span> <span class="token comment">-- 1</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">/</span> b<span class="token punctuation">)</span> <span class="token comment">-- 1.3333333333333</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">*</span> b<span class="token punctuation">)</span> <span class="token comment">-- 12</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">%</span> b<span class="token punctuation">)</span> <span class="token comment">-- 1</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">^</span> b<span class="token punctuation">)</span> <span class="token comment">-- 64.0</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="类型转换"><a aria-hidden="true" tabindex="-1" href="#类型转换"><span class="icon icon-link"></span></a>类型转换</h3><div class="wrap-body">
<ul class="style-round">
<li>
<p>在算术运算中，string 类型会尝试自动转换为 number 时</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c <span class="token operator">=</span> <span class="token string">"str"</span><span class="token punctuation">,</span> <span class="token string">"1"</span><span class="token punctuation">,</span> <span class="token string">"2"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- print(a + b) -- error</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>b <span class="token operator">+</span> c<span class="token punctuation">)</span> <span class="token comment">-- 3</span>
</span></code></pre>
</li>
<li>
<p>number 类型使用 <code>..</code> 会自动转换为 string</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> a<span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">type</span><span class="token punctuation">(</span>a <span class="token operator">..</span> b<span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
</li>
<li>
<p>其它方式的转换</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">type</span><span class="token punctuation">(</span><span class="token function">tostring</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">-- string</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">type</span><span class="token punctuation">(</span><span class="token function">tonumber</span><span class="token punctuation">(</span><span class="token string">"12"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">-- number</span>
</span></code></pre>
</li>
</ul>
<!--rehype:className=style-round-->
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="goto-语法"><a aria-hidden="true" tabindex="-1" href="#goto-语法"><span class="icon icon-link"></span></a>goto 语法</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> <span class="token keyword">function</span> <span class="token function">isValidNumber</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">if</span> <span class="token function">type</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span> <span class="token operator">~=</span> <span class="token string">"number"</span> <span class="token keyword">then</span>
</span><span class="code-line">        <span class="token keyword">goto</span> invalidNumber   <span class="token comment">-- 无条件地跳转到代码中的标签 `::invalidNumber::`</span>
</span><span class="code-line">    <span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Valid number:"</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token keyword">true</span>
</span><span class="code-line">
</span><span class="code-line">    <span class="token punctuation">::</span>invalidNumber<span class="token punctuation">::</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Invalid number:"</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token keyword">false</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">isValidNumber</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span>    <span class="token comment">-- 输出: Valid number: 123</span>
</span><span class="code-line"><span class="token function">isValidNumber</span><span class="token punctuation">(</span><span class="token string">"abc"</span><span class="token punctuation">)</span>  <span class="token comment">-- 输出: Invalid number: abc</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="条件语句"><a aria-hidden="true" tabindex="-1" href="#条件语句"><span class="icon icon-link"></span></a>条件语句</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="运算符-1"><a aria-hidden="true" tabindex="-1" href="#运算符-1"><span class="icon icon-link"></span></a>运算符</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<h4 id="关系运算符"><a aria-hidden="true" tabindex="-1" href="#关系运算符"><span class="icon icon-link"></span></a>关系运算符</h4>

































<table class="show-header"><thead><tr><th align="left">符号</th><th align="left">含义</th></tr></thead><tbody><tr><td align="left"><code>==</code></td><td align="left">等于</td></tr><tr><td align="left"><code>~=</code></td><td align="left">不等于</td></tr><tr><td align="left"><code>></code></td><td align="left">大于</td></tr><tr><td align="left"><code>&#x3C;</code></td><td align="left">小于</td></tr><tr><td align="left"><code>>=</code></td><td align="left">大于等于</td></tr><tr><td align="left"><code>&#x3C;=</code></td><td align="left">小于等于</td></tr></tbody></table>
<!--rehype:className=show-header-->
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> a<span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">&#x3C;</span> b<span class="token punctuation">)</span> <span class="token comment">-- false</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">&#x3C;=</span> b<span class="token punctuation">)</span> <span class="token comment">-- false</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">==</span> b<span class="token punctuation">)</span> <span class="token comment">-- false</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">~=</span> b<span class="token punctuation">)</span> <span class="token comment">-- true</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">></span> b<span class="token punctuation">)</span> <span class="token comment">-- true</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token operator">>=</span> b<span class="token punctuation">)</span><span class="token comment">-- true</span>
</span></code></pre>
<h4 id="逻辑运算符"><a aria-hidden="true" tabindex="-1" href="#逻辑运算符"><span class="icon icon-link"></span></a>逻辑运算符</h4>





















<table class="show-header"><thead><tr><th align="left">符号</th><th align="left">含义</th></tr></thead><tbody><tr><td align="left"><code>and</code></td><td align="left">逻辑与</td></tr><tr><td align="left"><code>or</code></td><td align="left">逻辑或操作符</td></tr><tr><td align="left"><code>not</code></td><td align="left">逻辑非操作符</td></tr></tbody></table>
<!--rehype:className=show-header-->
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> a<span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token keyword">true</span><span class="token punctuation">,</span> <span class="token keyword">false</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token keyword">and</span> b<span class="token punctuation">)</span> <span class="token comment">-- false</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token keyword">and</span> <span class="token keyword">not</span> b<span class="token punctuation">)</span> <span class="token comment">-- true</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a <span class="token keyword">or</span> b<span class="token punctuation">)</span> <span class="token comment">-- true</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="while-循环"><a aria-hidden="true" tabindex="-1" href="#while-循环"><span class="icon icon-link"></span></a>while 循环</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> num <span class="token operator">=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token keyword">while</span> <span class="token punctuation">(</span>num <span class="token operator">&#x3C;</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"num 的值为:"</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span>
</span><span class="code-line">  num <span class="token operator">=</span> num <span class="token operator">+</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="if-语句"><a aria-hidden="true" tabindex="-1" href="#if-语句"><span class="icon icon-link"></span></a>if 语句</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">if</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">then</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"0 为 true"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<h4 id="if--elseif--else"><a aria-hidden="true" tabindex="-1" href="#if--elseif--else"><span class="icon icon-link"></span></a>if .. elseif() .. else</h4>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> age <span class="token operator">=</span> <span class="token number">27</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">if</span> <span class="token punctuation">(</span>age <span class="token operator">&#x3C;</span> <span class="token number">18</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">then</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"age 小于 18"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">elseif</span> <span class="token punctuation">(</span>age <span class="token operator">&#x3C;</span> <span class="token number">25</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">then</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"age 小于 25"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">elseif</span> <span class="token punctuation">(</span>age <span class="token operator">&#x3C;</span> <span class="token number">30</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">then</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"age 小于 30"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">else</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"age 大于 30"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"age 的值为 :"</span><span class="token punctuation">,</span> age<span class="token punctuation">)</span>
</span></code></pre>
<p><red>注意: </red><code>Lua</code> 中 <code>0</code> 为 <code>true</code>，但是 <code>Lua</code> 中的 <code>nil</code> 可以当作 <code>false</code></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="for-循环"><a aria-hidden="true" tabindex="-1" href="#for-循环"><span class="icon icon-link"></span></a>for 循环</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">for</span> i <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<ul>
<li>lua 中的 for 循环从参数 1 变化到参数 2，每次变化以参数 3 为步长递增 i，并执行一次表达式</li>
<li>参数三，是可选的，如果不指定，默认是 1</li>
<li>参数二只会在一开始求值，其后不会再进行运算</li>
</ul>
<pre class="style-round wrap-text"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> f <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"in f(x) "</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">return</span> x <span class="token operator">*</span> <span class="token number">2</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">for</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<!--rehype:className=style-round wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="repeatuntil-循环"><a aria-hidden="true" tabindex="-1" href="#repeatuntil-循环"><span class="icon icon-link"></span></a>repeat...until 循环</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> num <span class="token operator">=</span> <span class="token number">11</span>
</span><span class="code-line"><span class="token keyword">repeat</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"num 的值为: "</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span>
</span><span class="code-line">  num <span class="token operator">=</span> num <span class="token operator">+</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token keyword">until</span> <span class="token punctuation">(</span>num <span class="token operator">></span> <span class="token number">10</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">-- num 的值为：11</span>
</span></code></pre>
<p><code>repeat...until</code> 循环的条件语句在当前循环结束后判断</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="break"><a aria-hidden="true" tabindex="-1" href="#break"><span class="icon icon-link"></span></a>break</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> num <span class="token operator">=</span> <span class="token number">11</span>
</span><span class="code-line"><span class="token keyword">repeat</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"num 的值为: "</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span>
</span><span class="code-line">  num <span class="token operator">=</span> num <span class="token operator">+</span> <span class="token number">1</span>
</span><span class="code-line">  <span class="token keyword">if</span> <span class="token punctuation">(</span>num <span class="token operator">></span> <span class="token number">15</span><span class="token punctuation">)</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token keyword">break</span>
</span><span class="code-line">  <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">until</span> <span class="token punctuation">(</span>num <span class="token operator">></span> <span class="token number">20</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="函数"><a aria-hidden="true" tabindex="-1" href="#函数"><span class="icon icon-link"></span></a>函数</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="初始化"><a aria-hidden="true" tabindex="-1" href="#初始化"><span class="icon icon-link"></span></a>初始化</h3><div class="wrap-body">
<p>像变量一样，如果加上 <code>local</code> 那么就是局部函数</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> <span class="token keyword">function</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"这是一个局部函数"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<hr>
<p>你也可以将函数赋值给一个变量</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> main <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"这是一个局部函数"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="返回值"><a aria-hidden="true" tabindex="-1" href="#返回值"><span class="icon icon-link"></span></a>返回值</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> <span class="token keyword">function</span> <span class="token function">min</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">if</span> <span class="token punctuation">(</span>a <span class="token operator">&#x3C;</span> b<span class="token punctuation">)</span> <span class="token keyword">then</span>
</span><span class="code-line">    <span class="token keyword">return</span> a
</span><span class="code-line">  <span class="token keyword">else</span>
</span><span class="code-line">    <span class="token keyword">return</span> b
</span><span class="code-line">  <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="参数"><a aria-hidden="true" tabindex="-1" href="#参数"><span class="icon icon-link"></span></a>参数</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> p <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"打印自己的风格"</span><span class="token punctuation">,</span> res<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">local</span> <span class="token keyword">function</span> <span class="token function">main</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> p<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">p</span><span class="token punctuation">(</span>a <span class="token operator">+</span> b<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">main</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> p<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多个返回值"><a aria-hidden="true" tabindex="-1" href="#多个返回值"><span class="icon icon-link"></span></a>多个返回值</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> <span class="token keyword">function</span> <span class="token function">min</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">local</span> sum <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">  <span class="token keyword">local</span> factorial <span class="token operator">=</span> <span class="token number">1</span>
</span><span class="code-line">  <span class="token keyword">for</span> i<span class="token punctuation">,</span> v <span class="token keyword">in</span> <span class="token function">pairs</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token keyword">do</span>
</span><span class="code-line">    sum <span class="token operator">=</span> sum <span class="token operator">+</span> v
</span><span class="code-line">    factorial <span class="token operator">=</span> factorial <span class="token operator">*</span> v
</span><span class="code-line">  <span class="token keyword">end</span>
</span><span class="code-line">  <span class="token keyword">return</span> sum<span class="token punctuation">,</span> factorial
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">local</span> a<span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="可变参数"><a aria-hidden="true" tabindex="-1" href="#可变参数"><span class="icon icon-link"></span></a>可变参数(<code>...</code>)</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> <span class="token keyword">function</span> <span class="token function">average</span><span class="token punctuation">(</span><span class="token punctuation">...</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">local</span> result <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">  <span class="token keyword">local</span> arg <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token punctuation">...</span> <span class="token punctuation">}</span>
</span><span class="code-line">  <span class="token keyword">for</span> i<span class="token punctuation">,</span> v <span class="token keyword">in</span> <span class="token function">ipairs</span><span class="token punctuation">(</span>arg<span class="token punctuation">)</span> <span class="token keyword">do</span>
</span><span class="code-line">    result <span class="token operator">=</span> result <span class="token operator">+</span> v
</span><span class="code-line">  <span class="token keyword">end</span>
</span><span class="code-line">  <span class="token keyword">return</span> result <span class="token operator">/</span> <span class="token operator">#</span>arg
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"平均值为"</span><span class="token punctuation">,</span> <span class="token function">average</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="字符串"><a aria-hidden="true" tabindex="-1" href="#字符串"><span class="icon icon-link"></span></a>字符串</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="字符串方法"><a aria-hidden="true" tabindex="-1" href="#字符串方法"><span class="icon icon-link"></span></a>字符串方法</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">-- 全部转换为大写</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">upper</span><span class="token punctuation">(</span><span class="token string">"str"</span><span class="token punctuation">)</span> <span class="token comment">-- STR</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 全部转换为小写</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">lower</span><span class="token punctuation">(</span><span class="token string">"STR"</span><span class="token punctuation">)</span> <span class="token comment">-- str</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 指定替换的字符串个数, 最后一个参数可选，默认是全部替换</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">gsub</span><span class="token punctuation">(</span><span class="token string">"aaaa"</span><span class="token punctuation">,</span> <span class="token string">"a"</span><span class="token punctuation">,</span> <span class="token string">"b"</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">-- bbba  3</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">gsub</span><span class="token punctuation">(</span><span class="token string">"Today is 29/01/2019"</span><span class="token punctuation">,</span> <span class="token string">"%d%d/%d%d/%d%d%d%d"</span><span class="token punctuation">,</span> <span class="token string">"good day."</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">-- Today is a good day. 1</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 查找第一个匹配的字符串，第三个参数可以提供开始查找的位置，默认从 1 开始</span>
</span><span class="code-line"><span class="token comment">-- 如果未找到，则返回 nil</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">"referference"</span><span class="token punctuation">,</span> <span class="token string">"fer"</span><span class="token punctuation">)</span> <span class="token comment">-- 3   5</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">"Today is 29/01/2021"</span><span class="token punctuation">,</span> <span class="token string">"%d%d/%d%d/%d%d%d%d"</span><span class="token punctuation">)</span> <span class="token comment">-- 10      19</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 字符串反转</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token string">"fw"</span><span class="token punctuation">)</span> <span class="token comment">-- wf</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 格式化字符串</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">"value:%c"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">-- value:a</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 转换字符并拼接</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">char</span><span class="token punctuation">(</span><span class="token number">97</span><span class="token punctuation">,</span><span class="token number">98</span><span class="token punctuation">,</span><span class="token number">99</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token comment">-- abcd</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 将字符转化为整数值。 int 用来指定某个字符，默认第一个字符</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">"ABCD"</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">-- 68</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 计算字符串长度</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">len</span><span class="token punctuation">(</span><span class="token string">"abc"</span><span class="token punctuation">)</span> <span class="token comment">-- 3</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 返回字符串的 n 个拷贝</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">rep</span><span class="token punctuation">(</span><span class="token string">"fw"</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token comment">-- fwfw</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 剪切字符串，第三个参数可选，默认是字符串长度</span>
</span><span class="code-line">string<span class="token punctuation">.</span><span class="token function">sub</span><span class="token punctuation">(</span><span class="token string">"referference"</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment">-- rf</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="正则匹配"><a aria-hidden="true" tabindex="-1" href="#正则匹配"><span class="icon icon-link"></span></a>正则匹配</h3><div class="wrap-body">

















































<table class="left-align"><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>%a</code></td><td align="left">与任何字母配对</td></tr><tr><td align="left"><code>%c</code></td><td align="left">与任何控制符配对(例如\n)</td></tr><tr><td align="left"><code>%d</code></td><td align="left">与任何数字配对</td></tr><tr><td align="left"><code>%l</code></td><td align="left">与任何小写字母配对</td></tr><tr><td align="left"><code>%p</code></td><td align="left">与任何标点(punctuation)配对</td></tr><tr><td align="left"><code>%s</code></td><td align="left">与空白字符配对</td></tr><tr><td align="left"><code>%u</code></td><td align="left">与任何大写字母配对</td></tr><tr><td align="left"><code>%w</code></td><td align="left">与任何字母/数字配对</td></tr><tr><td align="left"><code>%x</code></td><td align="left">与任何十六进制数配对</td></tr><tr><td align="left"><code>%z</code></td><td align="left">与任何代表0的字符配对</td></tr></tbody></table>
<!--rehype:className=left-align-->
<h4 id="match"><a aria-hidden="true" tabindex="-1" href="#match"><span class="icon icon-link"></span></a>match</h4>
<p>第三个参数可选，默认从 1 开始。如果没有捕获组返回整个字符串，匹配失败返回 nil</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line">string<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span>
</span><span class="code-line">  <span class="token string">"I have 2 questions for you."</span><span class="token punctuation">,</span> 
</span><span class="code-line">  <span class="token string">"(%d+) (%a+) "</span>
</span><span class="code-line"><span class="token punctuation">)</span>  <span class="token comment">-- 2       questions</span>
</span></code></pre>
<h4 id="gmatch"><a aria-hidden="true" tabindex="-1" href="#gmatch"><span class="icon icon-link"></span></a>gmatch</h4>
<p>返回一个迭代器函数，每次调用迭代器函数，如果参数 pattern 描述的字符串没有找到，迭代函数返回nil</p>
<pre class="style-round wrap-text"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">for</span> world <span class="token keyword">in</span> string<span class="token punctuation">.</span><span class="token function">gmatch</span><span class="token punctuation">(</span><span class="token string">"I have 2 questions for you."</span><span class="token punctuation">,</span> <span class="token string">"%a+"</span><span class="token punctuation">)</span> <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>world<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment">-- I</span>
</span><span class="code-line"><span class="token comment">-- have</span>
</span><span class="code-line"><span class="token comment">-- questions</span>
</span><span class="code-line"><span class="token comment">-- for</span>
</span><span class="code-line"><span class="token comment">-- you</span>
</span></code></pre>
<!--rehype:className=style-round wrap-text-->
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="数学方法"><a aria-hidden="true" tabindex="-1" href="#数学方法"><span class="icon icon-link"></span></a>数学方法</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="常用方法"><a aria-hidden="true" tabindex="-1" href="#常用方法"><span class="icon icon-link"></span></a>常用方法</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">-- 一个比任何数字都大的浮点数</span>
</span><span class="code-line">math<span class="token punctuation">.</span>huge
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 最小值的整数</span>
</span><span class="code-line">math<span class="token punctuation">.</span>mininteger
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">local</span> a <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">-- 1</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 返回不小于该数到最小整数</span>
</span><span class="code-line"><span class="token keyword">local</span> b <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span><span class="token number">1.2</span><span class="token punctuation">)</span> <span class="token comment">-- 2</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 返回不大于该数到最大整数</span>
</span><span class="code-line"><span class="token keyword">local</span> c <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span><span class="token number">1.2</span><span class="token punctuation">)</span> <span class="token comment">-- 1</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 取余</span>
</span><span class="code-line"><span class="token keyword">local</span> d <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">fmod</span><span class="token punctuation">(</span><span class="token number">9.9</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span> <span class="token comment">-- 0.9</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 返回最大值</span>
</span><span class="code-line"><span class="token keyword">local</span> g <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">-- 3</span>
</span><span class="code-line"><span class="token comment">-- 返回最小值</span>
</span><span class="code-line"><span class="token keyword">local</span> h <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">-- 1</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 返回参数的平方根</span>
</span><span class="code-line"><span class="token keyword">local</span> r <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">-- 9</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="工具方法"><a aria-hidden="true" tabindex="-1" href="#工具方法"><span class="icon icon-link"></span></a>工具方法</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">-- 返回数字的类型，</span>
</span><span class="code-line"><span class="token keyword">local</span> l <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">type</span><span class="token punctuation">(</span><span class="token number">1.2</span><span class="token punctuation">)</span> <span class="token comment">-- float</span>
</span><span class="code-line"><span class="token keyword">local</span> m <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">type</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">-- integer</span>
</span><span class="code-line"><span class="token keyword">local</span> n <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span> <span class="token comment">-- nil</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 返回以指定底底对数</span>
</span><span class="code-line"><span class="token keyword">local</span> e <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">-- 2</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 返回以 e 为底的自然对数</span>
</span><span class="code-line"><span class="token keyword">local</span> f <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">exp</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">-- 7.3890560989307</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 返回 [0,1) 区间内一致分布的浮点伪随机数</span>
</span><span class="code-line">math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">-- 返回 [1, n] 区间内一致分布的整数伪随机数</span>
</span><span class="code-line">math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">-- 返回 [n, m] 区间内一致分布的整数伪随机数</span>
</span><span class="code-line">math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 无符号整数比较，参数一 小于 参数二 则返回 true，否则返回 false</span>
</span><span class="code-line"><span class="token keyword">local</span> o <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">ult</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 如果参数可以转换为一个整数，则返回该整数，否则返回 nil</span>
</span><span class="code-line"><span class="token keyword">local</span> p <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">tointeger</span><span class="token punctuation">(</span><span class="token string">"3"</span><span class="token punctuation">)</span> <span class="token comment">-- 3</span>
</span><span class="code-line"><span class="token keyword">local</span> q <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">tointeger</span><span class="token punctuation">(</span><span class="token number">0.32</span><span class="token punctuation">)</span> <span class="token comment">-- nil</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 返回整数和小数部分</span>
</span><span class="code-line"><span class="token keyword">local</span> i<span class="token punctuation">,</span> j <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token function">modf</span><span class="token punctuation">(</span><span class="token number">3.14</span><span class="token punctuation">)</span> <span class="token comment">-- 3   0.14</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="其它方法"><a aria-hidden="true" tabindex="-1" href="#其它方法"><span class="icon icon-link"></span></a>其它方法</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">-- 圆周率</span>
</span><span class="code-line">math<span class="token punctuation">.</span>pi <span class="token comment">-- 3.1415926535898</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 正弦方法（以下皆是以弧度表示）</span>
</span><span class="code-line">math<span class="token punctuation">.</span><span class="token function">sin</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span>pi <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">-- 1.0</span>
</span><span class="code-line"><span class="token comment">-- 余弦方法</span>
</span><span class="code-line">math<span class="token punctuation">.</span><span class="token function">cos</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span>pi<span class="token punctuation">)</span> <span class="token comment">-- -1.0</span>
</span><span class="code-line"><span class="token comment">-- 正切方法</span>
</span><span class="code-line">math<span class="token punctuation">.</span><span class="token function">tan</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span>pi <span class="token operator">/</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">-- 1.0</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 反正弦方法（以下皆是以弧度表示）</span>
</span><span class="code-line">math<span class="token punctuation">.</span><span class="token function">acos</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">)</span> <span class="token comment">-- 0.0</span>
</span><span class="code-line"><span class="token comment">-- 反余弦方法</span>
</span><span class="code-line">math<span class="token punctuation">.</span><span class="token function">acos</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">)</span> <span class="token comment">-- 1.5707963267949</span>
</span><span class="code-line"><span class="token comment">-- 反正弦方法</span>
</span><span class="code-line">math<span class="token punctuation">.</span><span class="token function">atan</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">)</span> <span class="token comment">-- 0.78539816339745</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 角度转换为弧度</span>
</span><span class="code-line">math<span class="token punctuation">.</span><span class="token function">rad</span><span class="token punctuation">(</span><span class="token number">90</span><span class="token punctuation">)</span> <span class="token comment">-- 1.5707963267949</span>
</span><span class="code-line"><span class="token comment">-- 弧度转换为角度</span>
</span><span class="code-line">math<span class="token punctuation">.</span><span class="token function">deg</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span>pi<span class="token punctuation">)</span> <span class="token comment">-- 180.0</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="table-1"><a aria-hidden="true" tabindex="-1" href="#table-1"><span class="icon icon-link"></span></a>table</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="初始化数组"><a aria-hidden="true" tabindex="-1" href="#初始化数组"><span class="icon icon-link"></span></a>初始化数组</h3><div class="wrap-body">
<p>初始化一个空数组</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> array <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> 
</span></code></pre>
<hr>
<p>默认的数组索引从 1 开始</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> array <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">"a"</span><span class="token punctuation">,</span> <span class="token string">"b"</span><span class="token punctuation">,</span> <span class="token string">"c"</span><span class="token punctuation">,</span> <span class="token string">"d"</span> <span class="token punctuation">}</span>
</span><span class="code-line">array<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">"e"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">for</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span> <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多维数组"><a aria-hidden="true" tabindex="-1" href="#多维数组"><span class="icon icon-link"></span></a>多维数组</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> array <span class="token operator">=</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token punctuation">{</span> <span class="token string">"a"</span><span class="token punctuation">,</span> <span class="token string">"b"</span><span class="token punctuation">,</span> <span class="token string">"c"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token punctuation">{</span> <span class="token string">"d"</span><span class="token punctuation">,</span> <span class="token string">"e"</span><span class="token punctuation">,</span> <span class="token string">"f"</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">for</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">#</span>array <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token keyword">for</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">#</span>array<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span>array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="初始化-table"><a aria-hidden="true" tabindex="-1" href="#初始化-table"><span class="icon icon-link"></span></a>初始化 table</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> table <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line">table<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"fw"</span>
</span><span class="code-line">table<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token string">"18"</span>
</span><span class="code-line">table<span class="token punctuation">[</span><span class="token string">"sex"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">"boy"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 获取 table 的长度</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token operator">#</span>table<span class="token punctuation">)</span> <span class="token comment">-- 3</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 如果想要删除一个 table，那么可以使用 nil 赋值</span>
</span><span class="code-line">table <span class="token operator">=</span> <span class="token keyword">nil</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>table<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="table-方法"><a aria-hidden="true" tabindex="-1" href="#table-方法"><span class="icon icon-link"></span></a>table 方法</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">-- 用于连接 table 中指定的元素</span>
</span><span class="code-line"><span class="token comment">-- table.concat(table [, sep [, start [, end]]])</span>
</span><span class="code-line"><span class="token keyword">local</span> a <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">"apple"</span><span class="token punctuation">,</span> <span class="token string">"orange"</span><span class="token punctuation">,</span> <span class="token string">"peach"</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>table<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token string">"->"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">-- orange->peach</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- 用于向指定闻之插入元素。默认数组末尾</span>
</span><span class="code-line"><span class="token comment">-- table.insert(table, [pos,] value)</span>
</span><span class="code-line"><span class="token keyword">local</span> a <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">"apple"</span><span class="token punctuation">,</span> <span class="token string">"orange"</span><span class="token punctuation">,</span> <span class="token string">"peach"</span> <span class="token punctuation">}</span>
</span><span class="code-line">table<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"pear"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">-- pear</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- table.move(a1,f,e,t[,a2])</span>
</span><span class="code-line"><span class="token comment">-- 表a1，a1下标开始位置f，a1下标结束位置e，t选择移动到的开始位置(如果没有a2，默认a1的下标)</span>
</span><span class="code-line"><span class="token keyword">local</span> array <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">"a"</span><span class="token punctuation">,</span> <span class="token string">"b"</span><span class="token punctuation">,</span> <span class="token string">"c"</span> <span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">for</span> i<span class="token punctuation">,</span>v <span class="token keyword">in</span> <span class="token function">pairs</span><span class="token punctuation">(</span>table<span class="token punctuation">.</span><span class="token function">move</span><span class="token punctuation">(</span>array<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span> <span class="token comment">-- a a b c</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">-- table.sort (table [, comp])</span>
</span><span class="code-line"><span class="token comment">-- 排序，默认是升序</span>
</span><span class="code-line"><span class="token keyword">local</span> array <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">"a"</span><span class="token punctuation">,</span> <span class="token string">"c"</span><span class="token punctuation">,</span> <span class="token string">"b"</span> <span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">local</span> f <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">return</span> string<span class="token punctuation">.</span><span class="token function">byte</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token operator">-</span> string<span class="token punctuation">.</span><span class="token function">byte</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">table<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>array<span class="token punctuation">,</span> f<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">for</span> i<span class="token punctuation">,</span> v <span class="token keyword">in</span> <span class="token function">pairs</span><span class="token punctuation">(</span>array<span class="token punctuation">)</span> <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span> <span class="token comment">-- c b a</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="迭代器"><a aria-hidden="true" tabindex="-1" href="#迭代器"><span class="icon icon-link"></span></a>迭代器</h3><div class="wrap-body">
<h4 id="无状态的迭代器"><a aria-hidden="true" tabindex="-1" href="#无状态的迭代器"><span class="icon icon-link"></span></a>无状态的迭代器</h4>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">function</span> <span class="token function">square</span><span class="token punctuation">(</span>d<span class="token punctuation">,</span>n<span class="token punctuation">)</span>
</span><span class="code-line">   <span class="token keyword">if</span> n <span class="token operator">&#x3C;</span> d
</span><span class="code-line">   <span class="token keyword">then</span>
</span><span class="code-line">      n <span class="token operator">=</span> n <span class="token operator">+</span> <span class="token number">1</span>
</span><span class="code-line">   <span class="token keyword">return</span> n<span class="token punctuation">,</span> n<span class="token operator">*</span>n
</span><span class="code-line">   <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">for</span> i<span class="token punctuation">,</span>n <span class="token keyword">in</span> square<span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">0</span>
</span><span class="code-line"><span class="token keyword">do</span>
</span><span class="code-line">   <span class="token function">print</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span>n<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<h4 id="for-循环迭代器"><a aria-hidden="true" tabindex="-1" href="#for-循环迭代器"><span class="icon icon-link"></span></a>for 循环迭代器</h4>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">for</span> i<span class="token punctuation">,</span> n <span class="token keyword">in</span> <span class="token function">pairs</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> n<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="模块"><a aria-hidden="true" tabindex="-1" href="#模块"><span class="icon icon-link"></span></a>模块</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="定义模块"><a aria-hidden="true" tabindex="-1" href="#定义模块"><span class="icon icon-link"></span></a>定义模块</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">-- a.lua</span>
</span><span class="code-line"><span class="token keyword">local</span> mod <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line">mod<span class="token punctuation">.</span>cool <span class="token operator">=</span> <span class="token string">"this is a mod"</span>
</span><span class="code-line"><span class="token keyword">function</span> mod<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"this is a function"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">return</span> mod
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="导入模块"><a aria-hidden="true" tabindex="-1" href="#导入模块"><span class="icon icon-link"></span></a>导入模块</h3><div class="wrap-body">
<p>一般我们可以直接使用 <code>require</code> 导入</p>
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token comment">-- b.lua</span>
</span><span class="code-line"><span class="token comment">-- local mod = require("a")</span>
</span><span class="code-line"><span class="token comment">-- 使用 pcall 确保 require 函数导入成功，失败则返回一个 false 状态</span>
</span><span class="code-line"><span class="token keyword">local</span> status<span class="token punctuation">,</span> mod <span class="token operator">=</span> <span class="token function">pcall</span><span class="token punctuation">(</span>require<span class="token punctuation">,</span> <span class="token string">"a"</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">if</span> <span class="token keyword">not</span> status <span class="token keyword">then</span>
</span><span class="code-line">  <span class="token keyword">return</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">mod<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>mod<span class="token punctuation">.</span>cool<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="私有函数"><a aria-hidden="true" tabindex="-1" href="#私有函数"><span class="icon icon-link"></span></a>私有函数</h3><div class="wrap-body">
<pre class="language-lua"><code class="language-lua code-highlight"><span class="code-line"><span class="token keyword">local</span> mod <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">local</span> <span class="token keyword">function</span> <span class="token function">private</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"private"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">function</span> mod<span class="token punctuation">.</span><span class="token function">public</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">private</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">return</span> mod
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="http://www.lua.org">Lua 官网</a> <em>(lua.org)</em></li>
<li><a href="https://wiki.luatos.com/luaGuide/introduction.html">luatos</a> <em>(wiki.luatos.com)</em></li>
<li><a href="https://www.twle.cn/l/yufei/lua53/lua-basic-index.html">Lua 教程</a> <em>(twle.cn)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="../data.js?v=1.5.2" defer></script><script src="../js/fuse.min.js?v=1.5.2" defer></script><script src="../js/main.js?v=1.5.2" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
